package com.lesylphe.memoryboost;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private List<Defi> defis, defisPonderes;

	private List<List<Defi>> listeDefis;

	private static final String APPLI = "MEMOBOOST";

	private Defi defiCourant;

	private Score score;

	private int indexDefiCourant = 0;

	public Defi getDefiCourant() {
		return defiCourant;
	}

	public void setDefiCourant(Defi defiCourant) {
		this.defiCourant = defiCourant;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ajouterReponseListener();
		ajouterBonneReponseListener();
		ajouterMauvaiseReponseListener();
		demarrerNouvelleSerie();
	}

	private void demarrerNouvelleSerie() {
		score = new Score();
		initialiserDefis();
		indexDefiCourant = 0;
		setVisibility(R.id.buttonBonneReponse, false);
		setVisibility(R.id.buttonMauvaiseReponse, false);
		setVisibility(R.id.tvReponse, false);
		//
		effectuerNouveauChallenge();
	}

	private void initialiserListeDefis() {
		if (listeDefis == null) {
			listeDefis = new ArrayList<List<Defi>>();
			// départements
			List<Defi> dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("01",
					"Ain;Bourg-en-Bresse;Belley, Gex, Nantua"));
			dptsTmp.add(new Defi("02",
					"Aisne;Laon;Château-Thierry, Saint-Quentin, Soissons, Vervins"));
			dptsTmp.add(new Defi("03", "Allier;Moulins;Montluçon, Vichy"));
			dptsTmp.add(new Defi("04",
					"Alpes de Haute-Provence;Digne;Barcelonnette, Castellane, Forcalquier"));
			dptsTmp.add(new Defi("05", "Hautes Alpes;Gap;Briançon"));
			dptsTmp.add(new Defi("06", "Alpes Maritimes;Nice;Grasse"));
			dptsTmp.add(new Defi("07", "Ardèche;Privas;Largentière, Tournon"));
			dptsTmp.add(new Defi("08",
					"Ardennes;Charleville-Mézières;Rethel, Sedan, Vouzier"));
			dptsTmp.add(new Defi("09", "Ariège;Foix ;Pamiers, Saint-Giron"));
			dptsTmp.add(new Defi("10",
					"Aube;Troyes;Bar-sur-Aube, Nogent-sur-Seine"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("11", "Aude;Carcassonne;Limoux, Narbonne"));
			dptsTmp.add(new Defi("12",
					"Aveyron;Rodez;Millau, Villefranche-de-Rouergue"));
			dptsTmp.add(new Defi("13",
					"Bouches-du-Rhône;Marseille;Aix-en-Provence, Arles, Istres"));
			dptsTmp.add(new Defi("14", "Calvados;Caen;Bayeux, Lisieux, Vire"));
			dptsTmp.add(new Defi("15", "Cantal ;Aurillac ;Mauriac, Saint-Flour"));
			dptsTmp.add(new Defi("16", "Charente;Angoulême;Cognac, Confolens"));
			dptsTmp.add(new Defi(
					"17",
					"Charente-Maritime ;La Rochelle;Jonzac, Rochefort, Saint-Jean-d'Angely, Saintes"));
			dptsTmp.add(new Defi("18", "Cher ;Bourges;Saint-Amand-Montrond"));
			dptsTmp.add(new Defi("19",
					"Corrèze;Tulle;Brive-la-Gaillarde, Ussel"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("2A", "Corse du Sud;Ajaccio;Sartène"));
			dptsTmp.add(new Defi("2B", "Haute Corse;Bastia ;Calvi, Corte"));
			dptsTmp.add(new Defi("21", "Côte d'Or;Dijon;Beaune, Montbard"));
			dptsTmp.add(new Defi("22",
					"Côtes d'Armor;Saint-Brieuc;Dinan, Guingamp, Lannion"));
			dptsTmp.add(new Defi("23", "Creuse;Guéret;Aubusson"));
			dptsTmp.add(new Defi("24",
					"Dordogne;Périgueux;Bergerac, Nontron, Sarlat-la-Canéda"));
			dptsTmp.add(new Defi("25", "Doubs;Besançon;Montbéliard, Pontarlier"));
			dptsTmp.add(new Defi("26", "Drôme;Valence;Die, Nyons"));
			dptsTmp.add(new Defi("27", "Eure;Evreux;Les Andelys, Bernay"));
			dptsTmp.add(new Defi("28",
					"Eure-et-Loir;Chartres;Châteaudun, Dreux, Nogent-le-Rotrou"));
			dptsTmp.add(new Defi("29",
					"Finistère;Quimper;Brest, Châteaulin, Morlaix"));
			dptsTmp.add(new Defi("30", "Gard ;Nîmes;Alès, Le Vigan"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("31",
					"Haute-Garonne;Toulouse;Muret, Saint-Gaudens"));
			dptsTmp.add(new Defi("32", "Gers ;Auch;Condom, Mirande"));
			dptsTmp.add(new Defi("33",
					"Gironde;Bordeaux ;Blaye, Langon, Lesparre-Médoc, Libourne"));
			dptsTmp.add(new Defi("34", "Hérault;Montpellier;Béziers, Lodève"));
			dptsTmp.add(new Defi("35",
					"Ille-et-Villaine;Rennes;Fougères, Redon, Saint-Malo"));
			dptsTmp.add(new Defi("36",
					"Indre;Châteauroux;Issoudun, Le Blanc, La Châtre"));
			dptsTmp.add(new Defi("37", "Indre-et-Loire ;Tours ;Chinon, Loches"));
			dptsTmp.add(new Defi("38",
					"Isère ;Grenoble ;La Tour-du-Pin, Vienne"));
			dptsTmp.add(new Defi("39",
					"Jura;Lons-le-Saunier;Dole, Saint-Claude"));
			dptsTmp.add(new Defi("40", "Landes;Mont-de-Marsan;Dax"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("41",
					"Loir-et-Cher;Blois;Romorantin-Lanthenay, Vendôme"));
			dptsTmp.add(new Defi("42", "Loire;Saint-Étienne;Montbrison, Roanne"));
			dptsTmp.add(new Defi("43",
					"Haute-Loire ;Le Puy;Brioude, Yssingeaux"));
			dptsTmp.add(new Defi("44",
					"Loire Atlantique;Nantes;Ancenis, Châteaubriant, Saint-Nazaire"));
			dptsTmp.add(new Defi("45", "Loiret;Orléans;Montargis, Pithiviers"));
			dptsTmp.add(new Defi("46", "Lot;Cahors ;Figeac, Gourdon"));
			dptsTmp.add(new Defi("47",
					"Lot-et-Garonne;Agen;Marmande, Nérac, Villeneuve-sur-Lot"));
			dptsTmp.add(new Defi("48", "Lozère ;Mende ;Florac"));
			dptsTmp.add(new Defi("49",
					"Maine-et-Loire;Angers;Cholet, Saumur, Segré"));
			dptsTmp.add(new Defi("50",
					"Manche;Saint-Lô;Avranches, Cherbourg, Coutances"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("51",
					"Marne;Châlons-sur-Marne;Epernay, Reims, Sainte-Menehould, Vitry-le-François"));
			dptsTmp.add(new Defi("52",
					"Haute-Marne;Chaumont;Langres, Saint-Dizier"));
			dptsTmp.add(new Defi("53",
					"Mayenne;Laval ;Château-Gontier, Mayenne"));
			dptsTmp.add(new Defi("54",
					"Meurthe-et-Moselle;Nancy;Briey, Lunéville, Toul"));
			dptsTmp.add(new Defi("55", "Meuse;Bar-le-Duc;Commercy, Verdun"));
			dptsTmp.add(new Defi("56", "Morbihan ;Vannes;Lorient, Pontivy"));
			dptsTmp.add(new Defi("57",
					"Moselle;Metz ;Boulay-Moselle, Château-Salins, Forbach, Metz-Campagne"));
			dptsTmp.add(new Defi("58",
					"Nièvre;Nevers;Château-Chinon, Clamecy, Cosne-Cours-sur-Loire"));
			dptsTmp.add(new Defi("59",
					"Nord ;Lille;Avesnes-sur-Helpe, Cambrai, Douai, Dunkerque, Valenciennes"));
			dptsTmp.add(new Defi("60",
					"Oise ;Beauvais;Clermont, Compiègne, Senlis"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("61",
					"Orne;Alençon;Argentan, Mortagne-au-Perche"));
			dptsTmp.add(new Defi(
					"62",
					"Pas-de-Calais;Arras;Béthune, Boulogne-sur-Mer, Calais, Lens, Montreuil, Saint-Omer"));
			dptsTmp.add(new Defi("63",
					"Puy-de-Dôme;Clermont-Ferrand;Ambert, Issoire, Riom, Thiers"));
			dptsTmp.add(new Defi("64",
					"Pyrénées Atlantiques;Pau;Bayonne, Oloron-Sainte-Marie"));
			dptsTmp.add(new Defi("65",
					"Hautes Pyrénées;Tarbes;Argelès-Gazost, Bagnères-de-Bigorre"));
			dptsTmp.add(new Defi("66",
					"Pyrénées Orientales;Perpignan ;Céret, Prades"));
			dptsTmp.add(new Defi("67",
					"Bas Rhin;Strasbourg;Haguenau, Molsheim, Saverne, Sélestat, Wissembourg"));
			dptsTmp.add(new Defi("68",
					"Haut Rhin;Colmar ;Altkirch, Guebwiller, Mulhouse, Ribauvillé, Thann"));
			dptsTmp.add(new Defi("69", "Rhône ;Lyon ;Villefranche-sur-Saône"));
			dptsTmp.add(new Defi("70", "Haute Saône;Vesoul ;Lure"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("71",
					"Saône et Loire;Mâcon;Autun, Châlon-sur-Saône, Charolles, Louhan"));
			dptsTmp.add(new Defi("72", "Sarthe;Le Mans;La Flèche, Mamers"));
			dptsTmp.add(new Defi("73",
					"Savoie ;Chambéry;Albertville, Saint-Jean-de-Maurienne"));
			dptsTmp.add(new Defi("74",
					"Haute Savoie;Annecy;Bonneville, Saint-Julien-en-Genevois, Thonon-les-Bains"));
			dptsTmp.add(new Defi("75", "Ville de Paris;Paris;."));
			dptsTmp.add(new Defi("76", "Seine Maritime;Rouen;Dieppe, Le Havre"));
			dptsTmp.add(new Defi("77", "Seine et Marne;Melun;Meaux, Provins"));
			dptsTmp.add(new Defi("78",
					"Yvelines;Versailles ;Mantes-la-Jolie, Rambouillet, Saint-Germain-en-Laye"));
			dptsTmp.add(new Defi("79",
					"Deux Sèvres ;Niort;Bressuire, Parthenay"));
			dptsTmp.add(new Defi("80",
					"Somme;Amiens;Abbeville, Montdidier, Péronne"));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("81", "Tarn;Albi ;Castres"));
			dptsTmp.add(new Defi("82",
					"Tarn et Garonne;Montauban;Castelsarrasin"));
			dptsTmp.add(new Defi("83", "Var ;Toulon;Brignoles, Draguignan"));
			dptsTmp.add(new Defi("84", "Vaucluse;Avignon;Apt, Carpentras"));
			dptsTmp.add(new Defi("85",
					"Vendée;La Roche-sur-Yon;Fontenay-le-Comte, Sables-d'Olonne"));
			dptsTmp.add(new Defi("86",
					"Vienne ;Poitiers;Chatellerault, Montmorillon"));
			dptsTmp.add(new Defi("87",
					"Haute Vienne;Limoges;Bellac, Rochechouart"));
			dptsTmp.add(new Defi("88", "Vosges;Epinal ;Neufchâteau, Saint-Dié"));
			dptsTmp.add(new Defi("89", "Yonne;Auxerre;Avallon, Sens"));
			dptsTmp.add(new Defi("90", "Territoire de Belfort;Belfort;."));
			listeDefis.add(dptsTmp);
			dptsTmp = new ArrayList<Defi>();
			dptsTmp.add(new Defi("91", "Essonne;Evry;Etampes, Palaiseau"));
			dptsTmp.add(new Defi("92",
					"Hauts de Seine;Nanterre;Antony, Boulogne-Billancourt"));
			dptsTmp.add(new Defi("93", "Seine Saint-Denis ;Bobigny;Le Raincy"));
			dptsTmp.add(new Defi("94",
					"Val de Marne;Créteil ;L'Hay-les-Roses, Nogent-sur-Marne"));
			dptsTmp.add(new Defi("95",
					"Val d'Oise;Pontoise;Argenteuil, Montmorency"));
			dptsTmp.add(new Defi(
					"971",
					"Guadeloupe;Basse-Terre ;Pointe-à-Pitre, Saint-Martin, Saint-Barthélémy / Ministère de l'Outre-Mer "));
			dptsTmp.add(new Defi("972",
					"Martinique ;Fort-de-France;Le Marin, La Trinité"));
			dptsTmp.add(new Defi("973",
					"Guyane ;Cayenne ;Saint-Laurent-du-Maroni "));
			dptsTmp.add(new Defi("974",
					"Réunion ;Saint-Denis ;Saint-Benoît, Saint-Paul, Saint-Pierre"));
			dptsTmp.add(new Defi("975",
					"Saint-Pierre et Miquelon;Saint-Pierre ;Mairie"));
			dptsTmp.add(new Defi("976", "Mayotte;Dzaoudzi;Mayotte online "));
			dptsTmp.add(new Defi(
					"986",
					"Wallis et Futuna;Mata Utu (Île Uvea);Assemblée Territoriale des Iles Wallis et Futuna "));
			dptsTmp.add(new Defi(
					"987",
					"Polynésie Française;Papeete ;Îles du Vent, Îles Sous le Vent, Marquises , Australes, Tuamotu-Gambier Présidence de la Polynésie française "));
			dptsTmp.add(new Defi(
					"988",
					"Nouvelle-Calédonie;Nouméa;Haut-Commissariat de la République en Nouvelle-Calédonie / Gouvernement de la Nouvelle-Calédonie "));
			listeDefis.add(dptsTmp);
			// tables
			List<Defi> tables = new ArrayList<Defi>();
			tables.add(new Defi("5 x 1", "5"));
			tables.add(new Defi("5 x 2", "10"));
			tables.add(new Defi("5 x 3", "15"));
			tables.add(new Defi("5 x 4", "20"));
			tables.add(new Defi("5 x 5", "25"));
			tables.add(new Defi("5 x 6", "30"));
			tables.add(new Defi("5 x 7", "35"));
			tables.add(new Defi("5 x 8", "40"));
			tables.add(new Defi("5 x 9", "45"));
			tables.add(new Defi("5 x 10", "50"));
			listeDefis.add(tables);
			tables = new ArrayList<Defi>();
			tables.add(new Defi("6 x 1", "6"));
			tables.add(new Defi("6 x 2", "12"));
			tables.add(new Defi("6 x 3", "18"));
			tables.add(new Defi("6 x 4", "24"));
			tables.add(new Defi("6 x 5", "30"));
			tables.add(new Defi("6 x 6", "36"));
			tables.add(new Defi("6 x 7", "42"));
			tables.add(new Defi("6 x 8", "48"));
			tables.add(new Defi("6 x 9", "54"));
			tables.add(new Defi("6 x 10", "60"));
			listeDefis.add(tables);
			tables = new ArrayList<Defi>();
			tables.add(new Defi("7 x 1", "7"));
			tables.add(new Defi("7 x 2", "14"));
			tables.add(new Defi("7 x 3", "21"));
			tables.add(new Defi("7 x 4", "28"));
			tables.add(new Defi("7 x 5", "35"));
			tables.add(new Defi("7 x 6", "42"));
			tables.add(new Defi("7 x 7", "49"));
			tables.add(new Defi("7 x 8", "56"));
			tables.add(new Defi("7 x 9", "63"));
			tables.add(new Defi("7 x 10", "70"));
			listeDefis.add(tables);
			tables = new ArrayList<Defi>();
			tables.add(new Defi("8 x 1", "8"));
			tables.add(new Defi("8 x 2", "16"));
			tables.add(new Defi("8 x 3", "24"));
			tables.add(new Defi("8 x 4", "32"));
			tables.add(new Defi("8 x 5", "40"));
			tables.add(new Defi("8 x 6", "48"));
			tables.add(new Defi("8 x 7", "56"));
			tables.add(new Defi("8 x 8", "64"));
			tables.add(new Defi("8 x 9", "72"));
			tables.add(new Defi("8 x 10", "80"));
			listeDefis.add(tables);
			tables = new ArrayList<Defi>();
			tables.add(new Defi("9 x 1", "9"));
			tables.add(new Defi("9 x 2", "18"));
			tables.add(new Defi("9 x 3", "27"));
			tables.add(new Defi("9 x 4", "36"));
			tables.add(new Defi("9 x 5", "45"));
			tables.add(new Defi("9 x 6", "54"));
			tables.add(new Defi("9 x 7", "63"));
			tables.add(new Defi("9 x 8", "72"));
			tables.add(new Defi("9 x 9", "81"));
			tables.add(new Defi("9 x 10", "90"));
			listeDefis.add(tables);
			defis = getDefis(0);
		}
	}

	private void initialiserDefis() {
		initialiserListeDefis();
		defisPonderes = new ArrayList<Defi>();
		for (Defi defi : defis) {
			int delta = defi.getDeltaReponse();
			int nombreAjouts = 1;
			if (delta < 0) {
				nombreAjouts = 0 - (delta - 1);
			}
			for (int j = 0; j < nombreAjouts; j++) {
				defisPonderes.add(defi);
			}
		}
		Collections.shuffle(defisPonderes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i(APPLI, "On create options menu!");
		getMenuInflater().inflate(R.menu.main, menu);
		int itemNumber = 0;
		// raz
		menu.add(0, itemNumber++, 0, "Remettre à zéro");
		// départements
		for (int i = 0; i < 10; i++) {
			menu.add(0, itemNumber++, 0, "Départements " + ((i * 10) + 1)
					+ " à " + ((i + 1) * 10));
		}
		// tables
		for (int i = 10; i < 15; i++) {
			menu.add(0, itemNumber++, 0, "Multiplication par " + (i - 5));
		}
		return true;
	}

	/* Handles Item Selection */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			raz();
			bilan();
			break;
		default:
			defis = getDefis(item.getItemId());
			demarrerNouvelleSerie();
			break;
		}
		return true;
	}

	private List<Defi> getDefis(int itemId) {
		return listeDefis.get(itemId);
	}

	private void effectuerNouveauChallenge() {
		if ((indexDefiCourant < defis.size())
				&& (indexDefiCourant < defisPonderes.size())) {
			Defi c = defisPonderes.get(indexDefiCourant);
			indexDefiCourant++;
			setDefiCourant(c);
			setQuestion(c.getQuestionPlusTaux());
			setReponse(c.getReponse());
			setVisibility(R.id.buttonBonneReponse, false);
			setVisibility(R.id.buttonMauvaiseReponse, false);
			setVisibility(R.id.tvReponse, false);
		} else {
			bilan();
		}
	}

	private void setReponse(String reponse) {
		TextView tv = (TextView) findViewById(R.id.tvReponse);
		tv.setText(reponse);
	}

	private void setQuestion(String question) {
		TextView tv = (TextView) findViewById(R.id.tvQuestion);
		tv.setText(question);
	}

	private void setVisibility(int button2, boolean b) {
		View v = findViewById(button2);
		if (b) {
			v.setVisibility(View.VISIBLE);
		} else {
			v.setVisibility(View.GONE);
		}
	}

	private void bilan() {
		String compteRendu = creerCompteRendu();
		TextView tv = (TextView) findViewById(R.id.tvBilan);
		tv.setText(compteRendu);
		setVisibility(R.id.tvBilan, true);
		demarrerNouvelleSerie();
	}

	private void raz() {
		for (Defi d : defis) {
			d.raz();
		}
	}

	private String creerCompteRendu() {
		return "Votre score est : " + score.getCompteRendu();
	}

	private void fireReponse(boolean b, Defi currentChallenge) {
		currentChallenge.prendreEnCompteReponse(b);
		score.prendreEnCompteReponse(b);
	}

	private void ajouterBonneReponseListener() {
		Button b = (Button) findViewById(R.id.buttonBonneReponse);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				fireReponse(true, getDefiCourant());
				effectuerNouveauChallenge();
			}

		});

	}

	private void ajouterMauvaiseReponseListener() {
		Button b = (Button) findViewById(R.id.buttonMauvaiseReponse);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				fireReponse(false, getDefiCourant());
				effectuerNouveauChallenge();
			}

		});

	}

	private void ajouterReponseListener() {
		Button b = (Button) findViewById(R.id.buttonRepondre);
		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setVisibility(R.id.buttonBonneReponse, true);
				setVisibility(R.id.buttonMauvaiseReponse, true);
				setVisibility(R.id.tvReponse, true);
				setVisibility(R.id.tvBilan, false);
			}

		});

	}

}
